<template>
  <div>
    <vxe-modal v-model="isVisiable" width="45vw" show-footer :mask="false">
      <template #title>
        <span class="text-bold">自定义折点</span>
      </template>
      <template #default>
        <vxe-form ref="formRef" title-align="left" title-width="80" :data="formData" :loading="loading">
          <vxe-form-gather span="12">
            <vxe-form-item title="属" field="genusCode" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">属</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.genusCode" disabled placeholder="请输入属" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="细菌代号" field="whonetOrgCode" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">细菌代号</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.whonetOrgCode" disabled placeholder="请输入细菌代号" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="细菌名称" field="whonetOrgName" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">细菌名称</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.whonetOrgName" disabled placeholder="请输入细菌名称" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="检测方法" field="testMethod" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">检测方法</span>
              </template>
              <template #default="params">
                <!-- <vxe-input v-model="params.data.testMethod" placeholder="请输入检测方法" clearable></vxe-input> -->
                <VxePulldownSelectionWithNa style="width: 100%" :url="'/lis/bac/base/ast-test-mth/dict/findBacAstTestMthDictList'" @confirmClick="dropSelectConfirm" :tableColumn="[
                        { type: 'seq', title: '#', width: '30px' },
                        { field: 'code', title: '名称' },
                        { field: 'name', title: '编码' }
                      ]" :aimKeys="[
                        { aimKey: 'testMethod', rowKey: 'code' },
                        { aimKey: 'testMethodNa', rowKey: 'name' }
                      ]" :displayTexts="['name']" :displayTextObj="params.data.testMethodNa ? params.data.testMethodNa : ''" />
              </template>
            </vxe-form-item>
            <vxe-form-item title="年份" field="year" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">年份</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.year" placeholder="请输入年份" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="耐药(r)" field="r" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">耐药(r)</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.r" placeholder="请输入耐药" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="SDD" field="sdd" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">SDD</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.sdd" placeholder="请输入SDD" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="流行病学临界值" field="ecvEcoff" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">流行病学临界值</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.ecvEcoff" placeholder="请输入流行病学临界值" clearable></vxe-input>
              </template>
            </vxe-form-item>
          </vxe-form-gather>
          <vxe-form-gather span="12">
            <vxe-form-item title="是否设置属" field="s" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">是否设置属</span>
              </template>
              <template #default="params">
                <vxe-checkbox v-model="params.data.isGenus" content :checked-value="1" :unchecked-value="0"></vxe-checkbox>
              </template>
            </vxe-form-item>
            <vxe-form-item title="抗生素编号" field="whonetAbxCode" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">抗生素编号</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.whonetAbxCode" disabled placeholder="请输入抗生素编号" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="抗生素名称" field="antName" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">抗生素名称</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.antName" disabled placeholder="请输入抗生素名称" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="效价" field="potency" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">效价</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.potency" placeholder="请输入效价" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="折点" field="ref" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">折点</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.ref" placeholder="请输入折点" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="中介(i)" field="i" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">中介(i)</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.i" placeholder="请输入中介" clearable></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="敏感(s)" field="s" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">敏感(s)</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.s" placeholder="请输入敏感" clearable></vxe-input>
              </template>
            </vxe-form-item>
          </vxe-form-gather>
        </vxe-form>
      </template>
      <template #footer>
        <vxe-button type="text" class="button-white margin-right" @click="cancelEvent">关闭</vxe-button>
        <vxe-button type="text" class="button-blue" @click="confirmEvent" :loading="loading">确定</vxe-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, getCurrentInstance, defineProps } from 'vue';
import { Splitpanes, Pane } from 'splitpanes';
import { formatDateTime, IsEmpty } from '@/utils/common';
import VxeDictSelection from '@/components/VxeDictSelection.vue';
import VxePulldownSelectionWithNa from '@/components/VxePulldownSelectionWithNa.vue';
import { VXETable } from 'vxe-table';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';

let { proxy }: any = getCurrentInstance();
let isVisiable = ref(false);
const formRef = ref(null);
const loading = ref(false);
let drugResultId = ref(''); /* 药敏结果Id */
const formData = reactive({
  id: '',
  guidelines: 'AUser-defined',
  year: Number(formatDateTime(new Date(), 'yyyy')),
  testMethod: '',
  testMethodNa: '',
  potency: '',
  organismCode: null,
  serovarGroup: null,
  whonetOrgCode: '',
  whonetOrgName: '',
  speciesGroup: null,
  genusCode: '',
  genusGroup: '',
  familyCode: null,
  anaerobe: null,
  subkingdomCode: null,
  breakpointType: 'Human',
  host: 'Human',
  siteOfInfection: null,
  referenceTable: '',
  ref: '',
  whonetAbxCode: '',
  whonetTest: '',
  r: '',
  i: '',
  sdd: null,
  s: '',
  ecvEcoff: null,
  ecvEcoffTentative: false,
  dateEntered: null,
  dateModified: null,
  comments: null,
  antName: '',
  isGenus: 0
});

defineExpose({
  showModal /**打开选择方案的弹窗 */,
  editShowModal
});

/**页面初始化/切换显示隐藏 */
function showModal(obj, busId) {
  isVisiable.value = true;
  resetForm();
  if (obj) {
    formData.id = '';
    formData.testMethod = obj.testMethod;
    formData.testMethodNa = obj.testMethodNa;

    formData.whonetOrgCode = obj.germNo;
    formData.whonetOrgName = obj.whonetOrgName;

    formData.whonetAbxCode = obj.antNo;
    formData.antName = obj.antNa;
    formData.potency = obj.potency;
    formData.year = Number(formatDateTime(new Date(), 'yyyy'));
    formData.ref = obj.breakPoint;
  }
  drugResultId.value = busId;
}

/**页面初始化/切换显示隐藏 */
function editShowModal(obj, busId) {
  isVisiable.value = true;
  resetForm();
  if (obj && !IsEmpty(obj.id)) {
    const url = '/bac-break-point/findBreakPointOne';
    proxy.$http
      .get(url, { params: { breakPointId: obj.breakPointId } })
      .then((res: any) => {
        if (res.data) {
          Object.assign(formData, res.data);
          formData.testMethodNa = obj.testMethodNa;
          formData.whonetOrgName = obj.whonetOrgName;
          formData.antName = obj.antNa;
          if (!IsEmpty(formData.genusCode)) {
            formData.isGenus = 1;
          }
        }
      })
      .catch((err: any) => {
        VxeMessage(err, VxeNotifyType.error);
      });
  }
  drugResultId.value = busId;
}

/**重置form表单 */
function resetForm() {
  formRef.value?.reset();
  formData.isGenus = 0;
}

/**确认选择 */
async function confirmEvent() {
  //保存自定义折点
  loading.value = true;
  const res = await saveSelfBreakpoint();
  loading.value = false;
  proxy.$emit('confirmSave', { id: drugResultId.value });
  isVisiable.value = false;
}

/* 保存自定义折点 */

function saveSelfBreakpoint() {
  return new Promise((resolve, reject) => {
    let url = '';
    // 判断添加和编辑接口
    if (IsEmpty(formData.id)) {
      url = '/bac-break-point/addBreakPoint';
    } else {
      url = '/bac-break-point/updateBreakPoint';
    }
    proxy.$http
      .post(url, formData)
      .then((res: any) => {
        resolve(res);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}
/**关闭窗体 */
function cancelEvent() {
  isVisiable.value = false;
}
/**下拉选中 */
function dropSelectConfirm(e) {
  e.aimKeys.forEach((m) => {
    formData[m.aimKey] = e.row[m.rowKey];
  });
}
</script>

<style scoped lang="scss">
:deep(.vxe-input--inner) {
  border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
}

:deep(.vxe-textarea--inner) {
  border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
}

:deep(.vxe-button.type--button:not(.is--round)) {
  border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
}
</style>
